package org.puppet.game.base;

import android.content.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;

public class PuppetUtils {
	
	public static final Matrix matrix=new Matrix();
	public static Bitmap createBitmap(Resources res , int resId) {
		return BitmapFactory.decodeResource(res, resId);
	}
	
	public static Bitmap createBitmap(Resources res , int resId,int left,int top,int width,int height) {
		Bitmap bitmap = createBitmap(res , resId);
		return Bitmap.createBitmap(bitmap, left, top, width, height);
	}
	
	public static Bitmap createBitmap(Bitmap bitmapSrc ,int left,int top,int width,int height) {
		return Bitmap.createBitmap(bitmapSrc, left, top, width, height);
	}
	
	public static Bitmap createBitmap(Drawable drawable) {
		return createBitmap(drawable,0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
	}
	public static Bitmap createBitmap(Drawable drawable,int left,int top,int width,int height) {
		Bitmap bitmap = Bitmap.createBitmap(width, height, true);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(left, top, width, height);
		drawable.draw(canvas);
		return bitmap;
	}
	
	public static Bitmap mirrorBitmap(Bitmap bitmapSrc) {
		int width=bitmapSrc.width(),height=bitmapSrc.height();
		int length=width*height;
		int[] pixels=new int[length];
		int[] newPixels=new int[length];
		int color;
		bitmapSrc.getPixels(pixels, 0, width, 0, 0, width, height);
		for(int i=0; i < length ;i+=width){
			for(int j=i,end=i+width,endO=end;j < end ;j++){
				color=pixels[--endO];
//				int alpha = (color >> 24) & 0xff;
//				int blue = (color >> 16) & 0xff;
//				int green = (color >> 8) & 0xff;
//				int red = (color >> 0) & 0xff;
//				newPixels[j] = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
				newPixels[j] = color;
			}
		}
		Bitmap newBitmap=Bitmap.createBitmap(width,height,bitmapSrc.hasAlpha());
		newBitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
		return newBitmap;
	}
	

	
	public static boolean pointInRect(Point aPoint, float x, float y, int width, int height) {
		return aPoint.x > x && aPoint.x < x + width && aPoint.y > y && aPoint.y < y + height;
	}

}
